﻿using System;
using FirebirdSql.Data.FirebirdClient;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Data;

namespace RMSM
{
    [Serializable()]
    public class LanQuayPhimBO : Csla.BusinessBase<LanQuayPhimBO>
    {
        private Hashtable hstOldValue = new Hashtable();
        public Hashtable HstOldValue
        {
            get { return this.hstOldValue; }
            set { this.hstOldValue = value; }
        }

        #region Business Properties
        //LANQUAYPHIMID
        private int? _LanQuayPhimID = null;
        public int? LanQuayPhimID
        {
            get { return this._LanQuayPhimID; }
            set { this._LanQuayPhimID = value; PropertyHasChanged("LanQuayPhimID"); }
        }

        //HOPDONGID
        private int? _HopDongID = null;
        public int? HopDongID
        {
            get { return this._HopDongID; }
            set { this._HopDongID = value; PropertyHasChanged("HopDongID"); }
        }

        //NGAYCHUP
        private DateTime? _NgayChup = DateTime.Now;
        public DateTime? NgayChup
        {
            get { return this._NgayChup; }
            set { this._NgayChup = value; PropertyHasChanged("NgayChup"); }
        }

        //NGAYGIAO
        private DateTime? _NgayGiao = null;
        public DateTime? NgayGiao
        {
            get { return this._NgayGiao; }
            set { this._NgayGiao = value; PropertyHasChanged("NgayGiao"); }
        }

        //GHICHU
        private string _GhiChu = null;
        public string GhiChu
        {
            get { return this._GhiChu; }
            set { this._GhiChu = value; PropertyHasChanged("GhiChu"); }
        }

        //MINDATE
        private DateTime? _MinDate = DateTime.Now;
        public DateTime? MinDate
        {
            get { return this._MinDate; }
            set { this._MinDate = value; PropertyHasChanged("MinDate"); }
        }

        //MAXDATE
        private DateTime? _Maxdate = DateTime.Now;
        public DateTime? Maxdate
        {
            get { return this._Maxdate; }
            set { this._Maxdate = value; PropertyHasChanged("Maxdate"); }
        }

        //DELE
        private bool? _DELE = false;
        public bool? DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("DELE"); }
        }


        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        protected override object GetIdValue()
        {
            return _LanQuayPhimID.Value;
        }

        private static int newLanQuayPhimID = -1;
        private LanQuayPhimBO()
        {
            _LanQuayPhimID = newLanQuayPhimID--;
        }

        public LanQuayPhimBO(FbDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public LanQuayPhimBO(FbDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static LanQuayPhimBO NewRootLanQuayPhimBO()
        {
            return DataPortal.Create<LanQuayPhimBO>(new RootCriteria());
        }

        internal static LanQuayPhimBO NewChildLanQuayPhimBO()
        {
            return DataPortal.Create<LanQuayPhimBO>(new ChildCriteria());
        }

        public static LanQuayPhimBO GetRootLanQuayPhimBO(int _LanQuayPhimID)
        {
            return DataPortal.Fetch<LanQuayPhimBO>(new RootCriteria(_LanQuayPhimID));
        }

        public static LanQuayPhimBO GetRootLanQuayPhimBOByWhere(string where)
        {
            return DataPortal.Fetch<LanQuayPhimBO>(where);
        }

        internal static LanQuayPhimBO GetChildLanQuayPhimBO(FbDataReader dataReader)
        {
            return new LanQuayPhimBO(dataReader);
        }

        public static void DeleteLanQuayPhimBO(int _LanQuayPhimID)
        {
            DataPortal.Delete(new RootCriteria(_LanQuayPhimID));
        }

        public static void DeleteLanQuayPhimBO(int _LanQuayPhimID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_LanQuayPhimID, directDelete));
        }

        public static string GetIDLanQuayPhimBO()
        {
            return "LanQuayPhimID";
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int LanQuayPhimID;
            public bool directDelete = false;
            public RootCriteria(int _LanQuayPhimID)
            {
                this.LanQuayPhimID = _LanQuayPhimID;
                this.directDelete = false;
            }

            public RootCriteria(int _LanQuayPhimID, bool directDelete)
            {
                this.LanQuayPhimID = _LanQuayPhimID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int LanQuayPhimID;
            public bool directDelete = false;
            public ChildCriteria(int _LanQuayPhimID)
            {
                this.LanQuayPhimID = _LanQuayPhimID;
                this.directDelete = false;
            }

            public ChildCriteria(int _LanQuayPhimID, bool directDelete)
            {
                this.LanQuayPhimID = _LanQuayPhimID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "LanQuayPhimBO_B";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = "LANQUAYPHIMID = " + criteria.LanQuayPhimID;

                    using (FbDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                            Fetch(reader);
                    }
                }
            }
        }

        private void This_Fetch(string where)
        {
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "LanQuayPhimBO_B";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = where;

                    using (FbDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                            Fetch(reader);
                    }
                }
            }
        }

        private void Fetch(FbDataReader dataReader)
        {
            if (!(dataReader["LANQUAYPHIMID"] is DBNull))
                this._LanQuayPhimID = (int)dataReader["LANQUAYPHIMID"];
            if (!(dataReader["HOPDONGID"] is DBNull))
                this._HopDongID = (int)dataReader["HOPDONGID"];
            if (!(dataReader["NGAYCHUP"] is DBNull))
                this._NgayChup = (DateTime)dataReader["NGAYCHUP"];
            if (!(dataReader["NGAYGIAO"] is DBNull))
                this._NgayGiao = (DateTime)dataReader["NGAYGIAO"];
            if (!(dataReader["GHICHU"] is DBNull))
                this._GhiChu = ((string)dataReader["GHICHU"]).Trim();
            if (!(dataReader["MINDATE"] is DBNull))
                this._MinDate = (DateTime)dataReader["MINDATE"];
            if (!(dataReader["MAXDATE"] is DBNull))
                this._Maxdate = (DateTime)dataReader["MAXDATE"];
            if (!(dataReader["DELE"] is DBNull))
                this._DELE = (((string)dataReader["DELE"]).Trim() == "1");
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "LanQuayPhimBO_I";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@HOPDONGID", FbDbType.Integer).Value = this._HopDongID;
                    cmd.Parameters.Add("@NGAYCHUP", FbDbType.TimeStamp).Value = this._NgayChup;
                    cmd.Parameters.Add("@NGAYGIAO", FbDbType.TimeStamp).Value = this._NgayGiao;
                    cmd.Parameters.Add("@GHICHU", FbDbType.VarChar).Value = this._GhiChu;
                    cmd.Parameters.Add("@MINDATE", FbDbType.TimeStamp).Value = this._MinDate;
                    cmd.Parameters.Add("@MAXDATE", FbDbType.TimeStamp).Value = this._Maxdate;
                    cmd.Parameters.Add("@DELE", FbDbType.Char).Value = (this._DELE.Value ? "1" : "0");

                    FbParameter pLanQuayPhimID = new FbParameter("@LANQUAYPHIMID", FbDbType.Integer);
                    pLanQuayPhimID.Direction = ParameterDirection.ReturnValue;
                    cmd.Parameters.Add(pLanQuayPhimID);

                    cmd.ExecuteNonQuery();
                    this._LanQuayPhimID = Convert.ToInt32(cmd.Parameters["@LANQUAYPHIMID"].Value);
                }
            }
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "LanQuayPhimBO_U";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@LANQUAYPHIMID", FbDbType.Integer).Value = this._LanQuayPhimID;
                    cmd.Parameters.Add("@HOPDONGID", FbDbType.Integer).Value = this._HopDongID;
                    cmd.Parameters.Add("@NGAYCHUP", FbDbType.TimeStamp).Value = this._NgayChup;
                    cmd.Parameters.Add("@NGAYGIAO", FbDbType.TimeStamp).Value = this._NgayGiao;
                    cmd.Parameters.Add("@GHICHU", FbDbType.VarChar).Value = this._GhiChu;
                    cmd.Parameters.Add("@MINDATE", FbDbType.TimeStamp).Value = this._MinDate;
                    cmd.Parameters.Add("@MAXDATE", FbDbType.TimeStamp).Value = this._Maxdate;
                    cmd.Parameters.Add("@DELE", FbDbType.Char).Value = (this._DELE.Value ? "1" : "0");

                    cmd.ExecuteNonQuery();
                }
            }
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_LanQuayPhimID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    if (criteria.directDelete)
                    {
                        cmd.CommandText = "DELETE FROM LANQUAYPHIM WHERE LANQUAYPHIMID = " + criteria.LanQuayPhimID + ";";
                        cmd.ExecuteNonQuery();
                    }
                    else
                    {
                        cmd.CommandText = "UPDATE LANQUAYPHIM SET LANQUAYPHIM.DELE = '1' WHERE LANQUAYPHIMID = " + criteria.LanQuayPhimID + ";";
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_LanQuayPhimID.Value));
            MarkNew();
        }

        public void GetOldData()
        {
            Type t = this.GetType();
            MemberInfo[] mi = t.GetMembers();
            int length = mi.Length;
            string name = "";
            object objValue = null;
            string strValue = null;

            for (int i = 0; i < length; i++)
                if (mi[i].MemberType == MemberTypes.Property)
                {
                    name = mi[i].Name;
                    if (StaticUtil.Log_ValidColumn(name))
                    {
                        objValue = t.GetProperty(name).GetValue(this, null);
                        if (objValue != null)
                            strValue = objValue.ToString();
                        else
                            strValue = "";

                        hstOldValue.Add(name, strValue);
                    }
                }
        }
        #endregion Internal method
        //==========================================================================
    }
}